/*
 * @Author: Jimmy
 * @LastModifiedBy: Jimmy
 * @Date: 2024-05-25 16:45:02
 * @LastEditTime: 2024-07-07 15:00:04
 * @FilePath: /nest_study/server/src/modules/system/user/entities/user.entity.ts
 */
import { SYS_USER_TYPE, USER_SEX } from "src/common/enum/user";
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
import { Exclude, instanceToPlain } from 'class-transformer';
import { BaseEntity } from "src/common/entites/base";
import { SYS_STATUS } from "src/common/enum/status";

@Entity({
    name: 'sys_user',
    comment: "用户信息表"
})

export class SystemUserEntity extends BaseEntity {
    @PrimaryGeneratedColumn({
        type: 'int',
        name: "user_id",
        comment: "用户Id",
    })
    public id: number;

    @Column({
        type: 'varchar',
        name: 'user_name',
        length: 30,
        nullable: false,
        comment: '用户账号'
    })
    public userName: string;

    @Column('json', { nullable: true })
    public roleIds: number[];

    @Column('json', { nullable: true })
    public deptIds: number[];

    @Column('json', { nullable: true })
    public postIds: number[];

    @Column({
        type: 'varchar',
        name: 'nick_name',
        length: 30,
        nullable: false,
        comment: '用户昵称'
    })
    public nickName: string;

    @Column({
        type: 'varchar',
        name: 'user_type',
        length: 2,
        default: SYS_USER_TYPE.SYS,
        comment: '用户类型'
    })
    public userType: string

    @Column({
        type: 'varchar',
        name: 'status',
        length: 2,
        default: SYS_STATUS.COMMON,
        comment: '用户类型'
    })
    public status: string

    @Column({
        type: 'varchar',
        name: 'email',
        length: 50,
        default: '',
        comment: '邮箱'
    })
    public email: string

    @Column({
        type: 'varchar',
        name: 'phone',
        default: '',
        length: 11,
        comment: '手机号'
    })
    public phone: string;

    @Column({
        type: 'varchar',
        name: 'sex',
        length: 1,
        default: USER_SEX.MAN,
        comment: '性别'
    })
    public sex: string;

    @Column({
        type: 'varchar',
        name: 'avatar',
        default: '',
        comment: '用户头像'
    })
    public avatar: string;

    @Exclude({ toPlainOnly: true })//输出屏蔽密码
    @Column({
        type: 'varchar',
        length: 200,
        nullable: false,
        comment: '用户登录密码'
    })
    public password: string;

    @Column({
        type: 'varchar',
        name: 'login_ip',
        length: 128,
        default: '',
        comment: '用户登录IP'
    })
    public loginIp: string;

    @Column({
        type: 'timestamp',
        name: "login_time",
        nullable: true,
        comment: '登录时间'
    })
    public loginTime: Date

    toJSON() {
        return instanceToPlain(this);
    }
}
